package com.lzy.liujing.restaurant.controller.sysController;

import com.lzy.liujing.restaurant.entity.*;
import com.lzy.liujing.restaurant.service.CookAndServingService;
import com.lzy.liujing.restaurant.service.GoodsService;
import com.lzy.liujing.restaurant.service.OrderDetailService;
import com.lzy.liujing.restaurant.service.OrderService;
import com.lzy.liujing.restaurant.utils.ResultUtil;
import org.apache.catalina.User;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.util.ArrayList;
import java.util.List;

/**
 * 制菜上菜管理
 */

@RestController
@CrossOrigin
@RequestMapping("/detail")
public class CookAndServingController{
    @Autowired
    private OrderDetailService orderDetailService;
    @Autowired
    GoodsService goodsService;

    @GetMapping("/current/{current}/size/{size}")
    public Result getAllPage(@PathVariable Integer current, @PathVariable Integer size) {
        PageItem<OrderDetail> brandPageItem = orderDetailService.queryPage(current, size);
        List<OrderDetail> goodsList = brandPageItem.getItems();

        List<OrderDetailDis> goodsDisplayList = new ArrayList<>();
        for (OrderDetail goods : goodsList) {
            // 调用GoodsCategory服务获取对应的CategoryName
            Goods category = goodsService.getById(goods.getGoodsId());
//            if(goods.getStatus().equals("1")){
//                goods.setStatus("正在制作");
//            }else if(goods.getStatus().equals("0")){
//                goods.setStatus("未制作");
//            } else if(goods.getStatus().equals("2")){
//                goods.setStatus("制作完成");
//            }else if(goods.getStatus().equals("3")){
//                goods.setStatus("已上菜");
//            }
            // 创建一个新对象，用于展示，包含CategoryName
            OrderDetailDis goodsDisplay = new OrderDetailDis(
                    goods.getOdId(),
                    goods.getOrderId(),
                    category.getGoodsName(), // 使用CategoryName替换category_id
                    goods.getCount(),
                    goods.getStatus(),
                    goods.getDescription()

            );
            goodsDisplayList.add(goodsDisplay);
        }

        // 将原PageItem的其他属性保留，只替换数据列表
        PageItem<OrderDetailDis> displayPageItem = new PageItem<OrderDetailDis>(brandPageItem.getPageNumber(), brandPageItem.getPageSize(), goodsDisplayList);

        Result result = new Result(displayPageItem, "查询成功");
        return result;

    }


    @PostMapping("/current/{current}/size/{size}")
    public Result getCondPage(@PathVariable Integer current,
                              @PathVariable Integer size,
                              @RequestBody OrderDetail query) {
        //执行查询
        PageItem<OrderDetail> item = orderDetailService.queryConditionPage(query, current, size);
        List<OrderDetail> goodsList = item.getItems();

        List<OrderDetailDis> goodsDisplayList = new ArrayList<>();
        for (OrderDetail goods : goodsList) {
            // 调用GoodsCategory服务获取对应的CategoryName
            Goods category = goodsService.getById(goods.getGoodsId());
            // 创建一个新对象，用于展示，包含CategoryName
            OrderDetailDis goodsDisplay = new OrderDetailDis(
                    goods.getOdId(),
                    goods.getOrderId(),
                    category.getGoodsName(), // 使用CategoryName替换category_id
                    goods.getCount(),
                    goods.getStatus(),
                    goods.getDescription()

            );
            goodsDisplayList.add(goodsDisplay);
        }

        // 将原PageItem的其他属性保留，只替换数据列表
        PageItem<OrderDetailDis> displayPageItem = new PageItem<OrderDetailDis>(item.getPageNumber(), item.getPageSize(), goodsDisplayList);

        Result result = new Result(displayPageItem, "查询成功");
        return result;

    }

    @DeleteMapping("/orderdetails/{id}")
    public Result delete(@PathVariable Integer id) {
        boolean flag = orderDetailService.deleteById(id);
        if (flag) {
            return new Result(null, "删除成功 ！");
        } else {
            return Result.fail(500, "删除失败 ！");
        }

    }

    @RequestMapping("/orderdetail/update")
    // ResponseBody 将json字符串 转换为java 列表
    public Result updateRed(@RequestBody OrderDetail brand) {

        boolean updateBrand = orderDetailService.update(brand);
        if(updateBrand){
            return new Result(brand, "修改成功");
        } else {
            return Result.fail(500, "修改失败");
        }
    }
    @GetMapping("/orderdetails/{id}")
    public Result getOne(@PathVariable Integer id) {
        OrderDetail byId = orderDetailService.getById(id);
        if (byId == null) {
            return Result.fail(404, "查询不到该数据");
        }
        Result result = new Result(byId, "查询成功");
        return result;
    }

    @PostMapping("/orderdetails")
    public Result addBatch(@RequestBody List<OrderDetail> ordersToUpdate) {
        boolean addBrand = orderDetailService.insert(ordersToUpdate);
        if(addBrand){
            return new Result(addBrand, "添加成功");
        } else {
            return Result.fail(500, "添加失败");
        }
    }

    @RequestMapping("/orderdetail/updateCode")
    public Result updatCode(@RequestBody OrderDetail OrderUpdate) {
        boolean updateCode = orderDetailService.updataCode(OrderUpdate.getOdId(), OrderUpdate.getStatus());
        if(updateCode){
            return new Result(null, "修改成功");
        } else {
            return Result.fail(500, "修改失败");
        }

    }
}
